package com.sunfeng.chap4.test4;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/11/9 14:33
 */
public class Server {
    public static void main(String[] args) {
        try {
            char[] chars = new char[3];
            ServerSocket serverSocket = new ServerSocket(8088);
            System.out.println("server accept阻塞开始=" + System.currentTimeMillis());
            Socket socket = serverSocket.accept();
            System.out.println("server accept阻塞结束=" + System.currentTimeMillis());
            InputStream inputStream = socket.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

            System.out.println("server read阻塞开始=" + System.currentTimeMillis());
            // 客户端没有发送数据  就会一直阻塞在这里
            int readLength = inputStreamReader.read(chars);

            while (readLength!=-1){
                String string = new String(chars, 0, readLength);
                System.out.println(string);
                readLength = inputStreamReader.read(chars);
            }
            System.out.println("server read阻塞结束=" + System.currentTimeMillis());
            inputStreamReader.close();
            inputStream.close();

            socket.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
